
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Security.Permissions;
using System.Web.Compilation;
using System.Web.Hosting;
using System.Web.UI.WebControls;
using System;
using System.Web.DynamicData;
using NotAClue.ComponentModel.DataAnnotations;

namespace NotAClue.Web.DynamicData
{
    public class SecureFieldTemplateFactory : FieldTemplateFactory
    {
        public override IFieldTemplate CreateFieldTemplate(MetaColumn column,
            DataBoundControlMode mode,
            String uiHint)
        {
            var pageTemplate = FieldTemplateExtensionMethods.GetPageTemplate();
            var readOnlyIn = column.GetAttribute<ColumnReadOnlyInAttribute>();
            if (column.IsReadOnly || (readOnlyIn != null && (readOnlyIn.PageTemplate & pageTemplate) == pageTemplate))
                mode = DataBoundControlMode.ReadOnly;

            // code to fix caching issue
            if (column.IsReadOnly)
                mode = DataBoundControlMode.ReadOnly;

            return base.CreateFieldTemplate(column, mode, uiHint);
        }
    }
}